package com.zhong.gang.app.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;

import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.databinding.ItemShoufaBinding;
import com.zhong.gang.app.db.UserBean;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.applib.CheseListener;
import com.zhong.gang.applib.bean.MaterialBean;
import com.zhong.gang.applib.ui.chese.MaterialAct;
import com.zhong.gang.applib.ui.chese.MaterialTypeBean;

import java.util.Locale;

public class ShouFaItemView extends FrameLayout  implements CheseListener , TextWatcher {
    private ItemShoufaBinding binding;
    private MaterialTypeBean bean;
    private int type ,SteelMillId;
    private ItemListener listener;
    private double money;

    public MaterialTypeBean getBean() {
        return bean;
    }

    public void setListener(ItemListener listener) {
        this.listener = listener;
    }

    public ShouFaItemView(Context context, boolean isShow, MaterialBean bean) {
        super(context);
        View view =LayoutInflater.from(getContext()).inflate(R.layout.item_shoufa,null);
        binding = DataBindingUtil.bind(view);
        binding.danjia.setVisibility(isShow?VISIBLE:GONE);
        binding.danjialine.setVisibility(isShow?VISIBLE:GONE);
        binding.setItem(this);
        binding.select.setTag(true);
        binding.weight.setEnabled(false);
        binding.typeName.setText(bean.getMaterialTypeName());
        binding.moneny.setText(bean.getPrice()+"");
        binding.weight.setText(String.format(Locale.CHINA,"%.2f",(float)bean.getWeight()));
        binding.select.setVisibility(GONE);
        binding.moneny.setEnabled(false);
        binding.type.setEnabled(false);
        this.addView(view);
    }

    public ShouFaItemView(Context context, boolean isShow,int type,int SteelMillId,double money) {
        super(context);
        this.type = type;
        this.SteelMillId = SteelMillId;
        View view =LayoutInflater.from(getContext()).inflate(R.layout.item_shoufa,null);
        binding = DataBindingUtil.bind(view);
        binding.danjia.setVisibility(isShow?VISIBLE:GONE);
        binding.danjialine.setVisibility(isShow?VISIBLE:GONE);
        binding.setItem(this);
        binding.select.setTag(true);
        binding.weight.setEnabled(false);
        binding.select.setVisibility(VISIBLE);
        binding.type.setEnabled(true);
        this.addView(view);
        this.money = money;
        binding.moneny.addTextChangedListener(this);
    }

    public ItemShoufaBinding getBinding() {
        return binding;
    }

    public ImageView getSelect(){
        if (binding == null)
            return new ImageView(getContext());
        return binding.select;
    }

    /**
     * 点击事件
     * @param view
     */
    public void onViewClicked(View view){
        int i = view.getId();
        if (i == R.id.type) {//点击事件
            if (SteelMillId <= 0 || type <=  0)
                return;
            UserBean userBean = UserUtils.getIntent(getContext()).getUser();
            MaterialAct.openAct(getContext(), Api.materialTypeList,
                    SteelMillId+"",type+""
                    ,userBean.isLogin(),userBean.getToken(),userBean.getId()+"",UserUtils.getIntent(getContext()).getRoleId()+"",this);
        } else if (i == R.id.select) {
            boolean nowStatus = (boolean) binding.select.getTag();
            if (listener != null)
                listener.viewStatus(nowStatus);
        }
    }

    @Override
    public void onCheseBack(MaterialTypeBean bean) {
        this.bean = bean;
        binding.typeName.setText(bean.getName());
        if (bean.getMaterialTypePrice() != null){
            if (bean.getMaterialTypePrice().getSteelMillPrice() <= 0)
                binding.moneny.setHint("限价"+(bean.getMaterialTypePrice().getShippingPrice()));
            else {
                binding.moneny.setHint("限价"+(bean.getMaterialTypePrice().getSteelMillPrice() - money));
            }
        }
        binding.moneny.setText("");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (listener != null)
            listener.showView();
    }

    public interface ItemListener{
        void showView();
        void viewStatus(boolean isAdd);
    }
}
